package com.mathew.spring.appsp.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mathew.spring.appsp.model.Book;
import com.mathew.spring.appsp.model.BookMapper;
import com.mathew.spring.appsp.model.entity.UserInfo;
import com.mathew.spring.appsp.model.entity.UserInfoMapper;
import com.mathew.spring.appsp.model.entity.UserInfoReqDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

@RestController
public class UserInfoController {

    private UserInfoMapper userInfoMapper;
    private BookMapper bookMapper;

    @Autowired
    public UserInfoController(UserInfoMapper userInfoMapper, BookMapper bookMapper) {
        this.userInfoMapper = userInfoMapper;
        this.bookMapper = bookMapper;
    }

    @GetMapping("/userInfo/all")
    public List<UserInfo> getUserInfo() {
        List<UserInfo> list = userInfoMapper.selectList(null);
        Logger.getLogger("UserInfoController").log(Level.INFO, list.toString());
        return list;
    }

    @PostMapping("/userInfo/add")
    public Boolean addUserInfo(@RequestBody UserInfoReqDto reqDto) throws Exception {
        UserInfo info = new UserInfo();
        info.setName(reqDto.getName());
        info.setAddress(reqDto.getAddress());
        info.setEmail(reqDto.getEmail());
        return userInfoMapper.insert(info) > 0;
    }

    @GetMapping("/userInfo/page")
    public List<UserInfo> getUserInfoPage(UserInfoReqDto reqDto) {
        IPage<UserInfo> page = new Page<>(reqDto.getPageNum(), reqDto.getPageSize());
        userInfoMapper.selectPage(page, null);
        return page.getRecords();
    }
    @GetMapping("/userInfo/{id}")
    public Book getUserInfoById(@PathVariable("id") int id) {

        Book book = bookMapper.selectBook(id);
        return book;
    }
}
